﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VirtualOrg.Helper;
using VirtualOrg.DB;
using System.Data;

namespace VirtualOrg.Web.manage
{
    public partial class CreateTaskPopup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains(SiteConstants.CookieName))
            {
                Response.Redirect("~/User/Login.aspx");
            }

            if (!IsPostBack)
            {
                LoadInitDropDowns();
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            long userId = SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID;
            long? taskId = 0;
            DateTime? startDate = null;
            DateTime? endDate = null;
            try
            {
                using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
                {
                    if (!string.IsNullOrWhiteSpace(txtStartDate.Text))
                    {
                        startDate = Convert.ToDateTime(txtStartDate.Text);
                    }

                    if (!string.IsNullOrWhiteSpace(txtEndDate.Text))
                    {
                        endDate = Convert.ToDateTime(txtEndDate.Text);
                    }

                    dataContext.CreateTask(txtTitle.Text, ddlActivity.SelectedItem.Text, Convert.ToInt64(ddlAssignedTo.SelectedValue),
                                            Convert.ToInt16(ddlPriority.SelectedValue), txtDescription.Text, Convert.ToInt32(ddlProject.SelectedValue),
                                            userId, Convert.ToInt64(ddlOrganization.SelectedValue), startDate, endDate, ref taskId);

                    if (fileAttachment.HasFile)
                    {
                        string fileType = fileAttachment.FileName.Substring(fileAttachment.FileName.LastIndexOf('.'), fileAttachment.FileName.Length);
                        string fileName = Guid.NewGuid().ToString() + fileType;
                        string folderPath = "~/Content/TaskAttachment";
                        fileAttachment.SaveAs(Server.MapPath(string.Format("{0}/{1}", folderPath, fileName)));
                        dataContext.AddTaskAttachment(taskId, fileName, fileType);
                    }
                }

                Response.Redirect("~/manage/PopupMessage.aspx?t=TASK&e=1");
            }
            catch
            {
                Response.Redirect("~/manage/PopupMessage.aspx?t=TASK&e=0");
            }
        }

        private void LoadInitDropDowns()
        {
            long userId = SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID;
            using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
            {
                ddlOrganization.DataSource = dataContext.GetUserOrganizations(userId);
                ddlOrganization.DataTextField = "Title";
                ddlOrganization.DataValueField = "OrganizationID";
                ddlOrganization.DataBind();
            }

            ddlOrganization.Items.Insert(0, new ListItem("General", "0"));
            ddlProject.Items.Insert(0, new ListItem("General", "0"));
            ddlActivity.Items.Insert(0, new ListItem("Normal", "0"));
            ddlAssignedTo.Items.Insert(0, new ListItem(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).FullName, userId.ToString()));
            txtCreatedBy.Text = SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).FullName;

        }

        protected void ddlOrganization_SelectedIndexChanged(object sender, EventArgs e)
        {
            long orgID = Convert.ToInt64(ddlOrganization.SelectedValue);

            if (orgID > 0)
            {
                DataSet resultSet = DataHelper.GetTaskMasterData(orgID);

                if (resultSet.Tables[0].Rows.Count > 0)
                {
                    ddlActivity.DataSource = resultSet.Tables[0];
                    ddlActivity.DataTextField = "ItemTitle";
                    ddlActivity.DataValueField = "ItemID";
                    ddlActivity.DataBind();
                }

                if (resultSet.Tables[1].Rows.Count > 0)
                {
                    ddlAssignedTo.DataSource = resultSet.Tables[1];
                    ddlAssignedTo.DataTextField = "FullName";
                    ddlAssignedTo.DataValueField = "UserID";
                    ddlAssignedTo.DataBind();
                }

                if (resultSet.Tables[2].Rows.Count > 0)
                {
                    ddlProject.DataSource = resultSet.Tables[2];
                    ddlProject.DataTextField = "Title";
                    ddlProject.DataValueField = "ProjectID";
                    ddlProject.DataBind();
                    ddlProject.Items.Insert(0, new ListItem("General", "0"));
                }
            }
        }
    }
}